<template>
	<view>
		<u-cell-group>
			<u-cell-item icon="edit-pen-fill" title="修改密码" @click="handleToEditPassword"></u-cell-item>
			<u-cell-item icon="warning-fill" title="退出登录" @click="handleToLogout"></u-cell-item>
		</u-cell-group>
		<u-modal v-model="show" :content="content" :show-cancel-button="true" @confirm="handleToConfirmLogout"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		logout
	} from '../../api/user'
	export default {
		data() {
			return {
				show: false,
				content: '确定要退出吗'
			}
		},
		methods: {
			handleToLogout() {
				if (!this.show) {
					this.show = !this.show;
				}
			},
			handleToConfirmLogout() {
				this.handleLogout().then(res =>{
					this.show = !this.show;
					this.$refs.uToast.show({
						title: '注销账号成功',
						type: 'success',
						isTab: true,
						url: '/pages/user/user',
						duration:500,
						position:'top'
					})
				})
			},
			handleLogout(){
				return new Promise((resolve,reject) =>{
					let header = {
						'Authorization': 'Bearer ' + this.access_token
					}
					logout(header).then(res => {
						this.$u.vuex('access_token', '');
						this.$u.vuex('user_info', '');
						resolve('ok')
					}).catch(e =>{
						reject(e)
					})
				})
			},
			handleToEditPassword(){
				uni.navigateTo({
					url:'/pages/user/reset?type=0'
				})
			}
		}
	}
</script>

<style scoped>
	page {
		min-height: 100vh;
		background-color: #ededed;
	}
</style>
